package com.wjbgn.electronicnian.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wjbgn.electronicnian.convert.NianDoConvert;
import com.wjbgn.electronicnian.dto.NianDTO;
import com.wjbgn.electronicnian.dto.Result;
import com.wjbgn.electronicnian.entity.NianDO;
import com.wjbgn.electronicnian.service.NianService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Description:
 * Create Date: 2022-01-20T11:06:12.859722400
 *
 * @author weirx
 * @version 1.0
 */
@Api(tags = "")
@RestController
@RequestMapping("/nian")
@AllArgsConstructor
public class NianController {

    /**
     * NianService
     */
    private NianService nianService;

    /**
     * 分页列表
     *
     * @param nianDTO
     * @return
     */
    @ApiOperation(value = "分页列表")
    @GetMapping("/pageList")
    public Result pageList(NianDTO nianDTO) {
        QueryWrapper<NianDO> queryWrapper = new QueryWrapper<>(NianDoConvert.dtoToDo(nianDTO));
        Page<NianDO> page = new Page<>(nianDTO.getPage(), nianDTO.getLimit());
        Page<NianDO> pageList = nianService.page(page, queryWrapper);
        return Result.success(NianDoConvert.pageConvert(pageList));
    }

    /**
     * list列表
     *
     * @param nianDTO
     * @return
     */
    @ApiOperation(value = "list列表")
    @GetMapping("/list")
    public Result list(NianDTO nianDTO) {
        QueryWrapper<NianDO> queryWrapper = new QueryWrapper<>(NianDoConvert.dtoToDo(nianDTO));
        List<NianDO> nianList = nianService.list(queryWrapper);
        return Result.success(NianDoConvert.listConvert(nianList));
    }

    /**
     * 根据主键查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据主键查询")
    @GetMapping("/info/getById")
    public Result info(@RequestParam("id") Long id) {
        return nianService.getNian(id);
    }

    /**
     * 新增
     *
     * @param nianDTO
     * @return
     */
    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public Result save(@RequestBody NianDTO nianDTO) {
        boolean flag = nianService.save(NianDoConvert.dtoToDo(nianDTO));
        if (flag) {
            return Result.success();
        }
        return Result.failed();
    }

    /**
     * 更新
     *
     * @param nianDTO
     * @return
     */
    @ApiOperation(value = "根据id更新")
    @PostMapping("/updateById")
    public Result update(@RequestBody NianDTO nianDTO) {
        boolean flag = nianService.updateById(NianDoConvert.dtoToDo(nianDTO));
        if (flag) {
            return Result.success();
        }
        return Result.failed();
    }
}